package controllers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import models.Category;
import models.Deal;
import play.db.jpa.GenericModel.JPAQuery;

/**
 * Deal workflows, Note that the user need not be logged-in to access this as
 * this is just viewing or searchign deals
 * 
 */
public class Deals extends Application {

	/**
	 * Deal index (main page of deals)
	 */
	public static void index() {
		List<Deal> deals = new ArrayList();
		deals = Deal.find("order by endDate asc").fetch();
		renderTemplate("Application/index.html", deals);
	}
	
	/**
	 * REST Web Service method to display all the deals
	 */
	public static void dealService() {
		List<Deal> deals = new ArrayList();
		deals = Deal.find("order by endDate asc").fetch();
		renderJSON(deals);
	}

	/**
	 * View the deal deatils given the deal id
	 * 
	 * @param id
	 *            The deal id
	 */
	public static void viewDeal(Long id) {
		Deal deal = Deal.findById(id);
		render(deal);
	}

	

	/**
	 * Display the results of the search
	 * uses the query-string as how as the categoryId, passed within the params 
	 * 
	 */
	public static void displayResults() {
		
		
		String queryString = params.get("simpleSearch");
		String categoryIdString = params.get("categoryId");
		long categoryId = 0;
		
		JPAQuery query = null;
		
		if (queryString != null && !queryString.isEmpty()) {
			if (categoryIdString != null && !categoryIdString.isEmpty()) {
				categoryId = Long.valueOf(categoryIdString).longValue();
				query = Deal.find("select distinct d from Deal d, Address a, Category c where d.category = c.id and d.address = a.id and (d.title like ? or a.city like ?) and d.endDate >= ? and c.id = ? order by endDate asc",
						("%" + queryString + "%"), (queryString + "%"), (new Date()), categoryId);
				
			}
			else {
				query = Deal.find("select distinct d from Deal d, Address a where d.address = a.id and (d.title like ? or a.city like ?) and d.endDate >= ? order by endDate asc",
						("%" + queryString + "%"), (queryString + "%"), (new Date()));
				
			}
				
		}
		else if (categoryIdString != null && !categoryIdString.isEmpty()) {
			categoryId = Long.valueOf(categoryIdString).longValue();
			query = Deal.find("select distinct d from Deal d, Category c where d.category = c.id and d.endDate >= ? and c.id = ? order by endDate asc", new Date(), categoryId);
		}
		List<Deal> deals = query.fetch(10);
		
		
		
		renderTemplate("Application/index.html", deals, queryString, categoryId);

	}
	
	/**
	 * Retrieve and return the image associated with the deal from the database/file
	 * @param id
	 * 			The ID of the deal the image is associated with
	 */
	public static void dealPhoto(long id) {
		   final Deal deal = Deal.findById(id);
		   notFoundIfNull(deal);
		   if(null == deal.photo || deal.photo.get() == null){
			   return;
		   }
		   response.setContentTypeIfNotSet(deal.photo.type());
		   renderBinary(deal.photo.get());
	}

	
}
